﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DIProject.ReceptionGoods
{
   
	
    public partial class LinksMoron : Form
    {
        private string _goodName;
        /// <summary>
        /// Наименование товара поставщика
        /// </summary>
        public string GoodName
        {
            get { return _goodName; }
            set 
            {
                this.lblName.Text = value;
                this.textBox1.Text = value;
                _goodName = value; 
            }
        }
        private string _manufName;
        /// <summary>
        /// Наименование производителя
        /// </summary>
        public string ManufactureName
        {
            get { return _manufName; }
            set 
            {
                this.lbManufacrureName.Text = value;
                _manufName = value; 
            }
        }
        private string _sellerName;
        /// <summary>
        /// Наименование поставщика
        /// </summary>
        public string SellerName
        {
            get { return _sellerName; }
            set 
            {
                this.lbSellerName.Text = value;
                _sellerName = value; 
            }
        }
        private string _countryName;
        /// <summary>
        /// наименование страны поставщика
        /// </summary>
        public string CountryName
        {
            get { return _countryName; }
            set 
            {
                this.lblCountry.Text = value;
                _countryName = value; 
            }
        }
        private string _sellerGoodIdString;
        /// <summary>
        /// идентификатор товара поставщика
        /// </summary>
        public string SellerGoodIdString
        {
            get { return _sellerGoodIdString; }
            set 
            {
                this.lblIdSeller.Text = value;
                _sellerGoodIdString = value;
                _sellerGoodsId = Convert.ToInt32(value);
            }
        }

        private Int32 _sellerGoodsId;

        public Int32 SellerGoodsId
        {
            get { return _sellerGoodsId; }
            set { _sellerGoodsId = value; }
        }

        private DataSets.waybillsData.MoronRow _row;

        public DataSets.waybillsData.MoronRow MoronRow
        {
            get { return _row; }
            set 
            {
                if (value == null)
                    throw new Util.DIException("Не передана строка");
                this.GoodName = value.NAMLEK;
                this.ManufactureName = value.NAMZAVOD;
                this.CountryName = value.NAMSTRANA;
                this.SellerGoodIdString = Convert.ToInt32(value.KODNLKLEK).ToString();
                _row = value; 
            }
        }
        private Int32 _contragentId;

        public Int32 ContragentId
        {
            get { return _contragentId; }
            set { _contragentId = value; }
        }


        public LinksMoron(DataSets.waybillsData.MoronRow mr,Int32 contragentId)
        {
            InitializeComponent();
            MoronRow = mr;            
            DataSets.UtilTableAdapters.UtilQueries adapter = new DIProject.DataSets.UtilTableAdapters.UtilQueries();
            this.ContragentId = contragentId;
            this.SellerName = Convert.ToString(adapter.ContragentNameFromId(this.ContragentId));
        }
        /// <summary>
        /// выббранный нами внутренний товар
        /// </summary>
        public DataSets.waybillsData.vwGoodsDetailedRow SelectInnerGoodsRow
        {
            get
            {
                if (dgvGoods.SelectedRows.Count != 1)
                {
                    MessageBox.Show("Выделите строку", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return null;
                }
                DataGridViewRow dr = this.dgvGoods.SelectedRows[0];
                DataRowView drv = dr.DataBoundItem as DataRowView;
                return drv.Row as DataSets.waybillsData.vwGoodsDetailedRow;
            }
        }
        private DataSets.waybillsData.vwGoodsDetailedDataTable _data;

        public DataSets.waybillsData.vwGoodsDetailedDataTable gData
        {
            get 
            {
 
                return _data; 
            }
            
            set 
            { 
                _data = value;
                if (value == null)
                {
                    Data.vwGoodsDetailed.ClearData();
                }
                dgvGoods.DataSource = _data;
            }
        }
        private DataView DataView
        {
            get
            {
                if (_data != null)
                    return _data.DefaultView;
                throw new Util.DIException("Не инициализированы данные!");
            }
        }
        private string DataFilter
        {
            get
            {
                return this.DataView.RowFilter;
            }
            set
            {
                this.DataView.RowFilter = value;
            }
        }
        private void LinksMoron_Load(object sender, EventArgs e)
        {
            //(this.dgvGoods as ISupportInitialize).BeginInit();
            this.gData  = Data.vwGoodsDetailed.Data;
           // (this.dgvGoods as ISupportInitialize).EndInit();
        }
        private void ClearFilter()
        {
            this.DataFilter = string.Empty;
        }
        private void txbFiltrName_TextChanged(object sender, EventArgs e)
        {
            timerFilter.Stop();
            timerFilter.Start();
        }

        private void timerFilter_Tick(object sender, EventArgs e)
        {
            timerFilter.Stop();
            this.SuspendLayout();
            this.DataFilter = "FullName LIKE '" + txbFiltrName.Text + "*'";
            this.ResumeLayout();
            

        }

        private void LinksMoron_FormClosed(object sender, FormClosedEventArgs e)
        {
            timerFilter.Stop();
        }

        private void dgvGoods_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex < 0)
                return;
            this.dgvGoods.Rows[e.RowIndex].Selected = true;
        }

        private void btnLink_Click(object sender, EventArgs e)
        {
            NewLink();
            this.DialogResult = DialogResult.OK;
        }
        /// <summary>
        /// создаем новую перекодировку
        /// </summary>
        private void NewLink()
        {
            if (this.dgvGoods.SelectedRows.Count < 1)
            {
                MessageBox.Show("Выделите строку","Ошибка",MessageBoxButtons.OK,MessageBoxIcon.Error);
                return;
            }
            
            Data.Links.AddNewLink(SelectInnerGoodsRow.Id, this.ContragentId, this.SellerGoodsId,
                this.GoodName.Replace("@", string.Empty), this.CountryName.Replace("@", string.Empty), this.ManufactureName.Replace("@", string.Empty), null);
        }

        private void linkToolStripMenuItem_Click(object sender, EventArgs e)
        {
            NewLink();
        }

        private void bntNewGood_Click(object sender, EventArgs e)
        {
            Dictionary.NewGoods ng = new DIProject.Dictionary.NewGoods();
            ng.MdiParent = this.MdiParent;
            DialogResult dr = ng.ShowDialog();
            if (dr == DialogResult.OK)
            {
                Data.vwGoodsDetailed.ClearData();
                this.gData = Data.vwGoodsDetailed.Data;
            }
        }

        private void LinksMoron_Shown(object sender, EventArgs e)
        {
            string tmpf = this.GoodName;
            int indexof = tmpf.IndexOf(' ', 0);
            if (indexof == -1) indexof = tmpf.Length;
            tmpf = tmpf.Substring(0, indexof);
            txbFiltrName.Text = tmpf;
        }
    }
}